# config section
PROGRAM = exec_plan

CXX = g++
GXX = g++
# debug
#CXXFLAGS = -Wall -Werror -lprotobuf -lpthread -g
# release
CXXFLAGS = -Wall -lprotobuf -lpthread -DNDEBUG -march=native

SRC = main.cc server.cc Parser.cc ScanOperationNode.cc ComputeOperationNode.cc FilterOperationNode.cc GroupByOperationNode.cc RowAggregator.cc Consumer.cc operations.pb.cc
GENH = operations.pb.h

# build section
OBJ = $(SRC:.cc=.o)
DEP = $(SRC:.cc=.d)

.PHONY: clean all test
.SUFFIXES:

# $(call make-depend,source-file,object-file,depend-file)
define make-depend
$(GXX) -MM -MF $3 -MP -MT $2 $(CXXFLAGS) $1
endef

all: $(PROGRAM)

$(PROGRAM): $(OBJ)
	g++ -o $(PROGRAM) $(OBJ) $(CXXFLAGS) 

test: $(PROGRAM) unit_tests tester
	./unit_tests && ./tester

unit_tests: unit_tests.cc $(GENH) $(OBJ:main.o=)
	g++ -o unit_tests $(CXXFLAGS) unit_tests.cc $(OBJ:main.o=)

hash_test: hash_test.cc hash.h
	g++ -o hash_test $(CXXFLAGS) hash_test.cc

%.o: %.cc $(GENH)
	$(call make-depend,$<,$@,$(subst .o,.d,$@))
	$(CXX) $(CXXFLAGS) -c $<

%.pb.cc %.pb.h: %.proto
	protoc $< --cpp_out .

clean:
	rm -f $(OBJ) $(DEP) $(PROGRAM) queries/q*.out *.pb.h *.pb.cc unit_tests hash_test
	
ifneq "$(MAKECMDGOALS)" "clean"
-include $(DEP)
endif
